<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>从数组中取出一段</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.array-shift.html">« array_shift</a></li>
      <li style="float: right;"><a href="function.array-splice.html">array_splice »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.array.html">数组 函数</a></li>
    <li>从数组中取出一段</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.array-slice" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">array_slice</h1>
    <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">array_slice</span> &mdash; <span class="dc-title">从数组中取出一段</span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.array-slice-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>array_slice</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">array</span> <code class="parameter">$array</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$offset</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">?</span><span class="type"><span class="type">int</span><span class="type"></span></span> <code class="parameter">$length</code><span class="initializer"> = <strong><code>null</code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">bool</span> <code class="parameter">$preserve_keys</code><span class="initializer"> = <strong><code>false</code></strong></span></span><br>): <span class="type">array</span></div>

    <p class="para rdfs-comment">
     <span class="function"><strong>array_slice()</strong></span> 返回根据 <code class="parameter">offset</code> 和
     <code class="parameter">length</code> 参数所指定的 <code class="parameter">array</code>
     数组中的一段序列。
    </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.array-slice-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">array</code></dt>

     <dd>

      <p class="para">
       输入的数组。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">offset</code></dt>

     <dd>

      <p class="para">
        如果 <code class="parameter">offset</code> 非负，则序列将从 <code class="parameter">array</code>
     中的此偏移量开始。
      </p>
      <p class="para">
       如果 <code class="parameter">offset</code> 为负，则序列将从
     <code class="parameter">array</code> 中距离末端这么远的地方开始。
      </p>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <p class="para">
        参数 <code class="parameter">offset</code> 标识的是数组中的位置，而不是键。
       </p>
      </p></blockquote>
     </dd>

    
    
     <dt>
<code class="parameter">length</code></dt>

     <dd>

      <p class="para">
       如果给出了 <code class="parameter">length</code> 并且为正，则序列中将具有这么多的单元。
      </p>
      <p class="para">
       如果 array 比 <code class="parameter">length</code> 要短，只会保留有效的数组单元。
      </p>
      <p class="para">如果给出了
     <code class="parameter">length</code> 并且为负，则序列将终止在距离数组末端这么远的地方。
      </p>
      <p class="para">如果省略，则序列将从
     <code class="parameter">offset</code> 开始一直到 <code class="parameter">array</code> 的末端。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">preserve_keys</code></dt>

     <dd>

      <blockquote class="note"><p><strong class="note">注意</strong>: 
      <p class="para">
        注意 <span class="function"><strong>array_slice()</strong></span> 默认会重新排序并重置数组的数字索引。你可以通过将
     <code class="parameter">preserve_keys</code> 设为 <strong><code>true</code></strong> 来改变此行为。
       无论本参数如何设置，都会保留字符串的键。
      </p>
      </p></blockquote>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.array-slice-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回其中一段。 如果 offset 参数大于 array 尺寸，就会返回空的 array。
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.array-slice-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4953">
    <p><strong>示例 #1 <span class="function"><strong>array_slice()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$input&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"a"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"b"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"c"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"d"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"e"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$output&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_slice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;返回&nbsp;"c"，&nbsp;"d"&nbsp;和&nbsp;"e"<br /></span><span style="color: #0000BB">$output&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_slice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;返回&nbsp;"d"<br /></span><span style="color: #0000BB">$output&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_slice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;返回&nbsp;"a"，&nbsp;"b"&nbsp;和&nbsp;"c"<br /><br />//&nbsp;注意数组中&nbsp;key&nbsp;的不同<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">array_slice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">array_slice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; c
    [1] =&gt; d
)
Array
(
    [2] =&gt; c
    [3] =&gt; d
)
</pre></div>
    </div>
   </div>
  </p>
  
  <p class="para">
   <div class="example" id="example-4954">
    <p><strong>示例 #2 <span class="function"><strong>array_slice()</strong></span> 于索引从 1 开始的 array</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$input&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"a"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"b"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"c"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"d"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"e"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">array_slice</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; b
    [1] =&gt; c
)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4955">
    <p><strong>示例 #3 <span class="function"><strong>array_slice()</strong></span> 与混合类型键的 array</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$ar&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'a'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'b'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'banana'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'42'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'pear'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'d'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'orange'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">array_slice</span><span style="color: #007700">(</span><span style="color: #0000BB">$ar</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">array_slice</span><span style="color: #007700">(</span><span style="color: #0000BB">$ar</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [a] =&gt; apple
    [b] =&gt; banana
    [0] =&gt; pear
)
Array
(
    [a] =&gt; apple
    [b] =&gt; banana
    [42] =&gt; pear
)
</pre></div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.array-slice-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.array-splice.html" class="function" rel="rdfs-seeAlso">array_splice()</a> - 去掉数组中的某一部分并用其它值取代</span></li>
    <li class="member"><span class="function"><a href="function.unset.html" class="function" rel="rdfs-seeAlso">unset()</a> - 清除指定变量</span></li>
    <li class="member"><span class="function"><a href="function.array-chunk.html" class="function" rel="rdfs-seeAlso">array_chunk()</a> - 将一个数组分割成多个</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>